from common.Utils import SHPDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

#测试商城系统购物车测试
class CartPage:
    def __init__(self):
        self.url = "http://49.235.61.184/"
        self.driver = SHPDriver.driver
        self.driver.get(self.url)
        self.wait = WebDriverWait(self.driver, 15)  # 全局显式等待（适配详情页加载慢）

    def cartTest1(self):
        # 1. 登录校验（购物车需登录访问）
        login_success = SHPDriver.Auto_Login()
        Get_cartpage=self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a"))
        )
        Get_cartpage.click()
        # 页面元素查找（显式等待，确保元素存在）
        # 删除按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > a.am-margin-left-main.nav-delete-submit.am-color-grey-dark.submit-ajax"))
        )
        # 收藏按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > a.am-margin-left-main.nav-collect-submit.am-color-grey-dark.submit-ajax"))
        )
        # 结算按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-right.am-fr.am-padding-right > form > button"))
        )
        # 全选按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > label > span.el-text.am-color-grey-dark"))
        )
        # 商品数量设置框
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "td.number"))
        )

        # 截图
        SHPDriver.GetScreenShot("cart_check_elements")

        # 功能测试
        # 减少和增加购物车商品的数量
        # 增加数量
        add_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "span.stock-submit[data-type='add']"))
        )
        add_btn.click()
        # 减少数量
        min_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "span.stock-submit[data-type='min']"))
        )
        min_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("cart_adjust_quantity")

        # 收藏和取消收藏购物车的该商品
        collect_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "td.operation > div > a.am-color-grey-dark.submit-ajax"))
        )
        collect_btn.click()  # 收藏
        collect_btn.click()  # 取消收藏
        time.sleep(1)
        SHPDriver.GetScreenShot("cart_collect_toggle")

        # 收藏全部购物车商品
        # 全选商品
        select_all_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > label > span.el-text.am-color-grey-dark"))
        )
        select_all_btn.click()
        # 批量收藏
        batch_collect_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > a.am-margin-left-main.nav-collect-submit.am-color-grey-dark.submit-ajax"))
        )
        batch_collect_btn.click()
        time.sleep(1)
        select_all_btn.click()
        SHPDriver.GetScreenShot("cart_batch_collect")

        # 结算商品（不测试支付，仅验证跳转）
        # 批量选择结算
        #点击结算
        checkout_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-right.am-fr.am-padding-right > form > button"))
        )
        select_all_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > label > span.el-text.am-color-grey-dark"))
        )
        select_all_btn.click()
        checkout_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("cart_batch_checkout")
        # 返回购物车页面
        self.driver.back()
        self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "td.number")))

        # 7. 删除商品
        # 7.1 获取删除前数量
        cart_count_before_element = self.wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
                                              "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div"))
        )
        cart_count_before_text = cart_count_before_element.text
        cart_count_before = int(''.join(filter(str.isdigit, cart_count_before_text))) if any(
            char.isdigit() for char in cart_count_before_text) else 0
        print(f"删除前购物车数量（提取后）：{cart_count_before}")
        SHPDriver.GetScreenShot("cart_before_delete")

        # 7.2 删除单个商品
        delete_single_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, ".am-color-grey-dark.submit-delete"))
        )
        delete_single_btn.click()
        confirm_delete_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, ".am-modal-footer > span:nth-child(2)"))
        )
        confirm_delete_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("cart_delete_single")

        # 7.3 全部删除商品
        batch_select_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > label > span.am-ucheck-icons > i.am-icon-checked"))
        )
        batch_select_btn.click()
        batch_delete_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.cart-content > div.cart-nav > div > div.nav-left.am-fl.am-padding-left > a.am-margin-left-main.nav-delete-submit.am-color-grey-dark.submit-ajax"))
        )
        batch_delete_btn.click()
        confirm_batch_delete_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, ".am-modal-footer > span:nth-child(2)"))
        )
        confirm_batch_delete_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("cart_delete_all")

        # 7.4 获取删除后数量
        cart_count_after_element = self.wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR,
                                              "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div"))
        )
        cart_count_after_text = cart_count_after_element.text
        cart_count_after = int(''.join(filter(str.isdigit, cart_count_after_text))) if any(
            char.isdigit() for char in cart_count_after_text) else 0
        print(f"删除后购物车数量（处理后）：{cart_count_after}")

        # 7.5 优化断言（确保从有商品变为空）
        assert cart_count_before > 0 and cart_count_after == 0, \
            f"购物车删除功能异常！删除前：{cart_count_before}，删除后：{cart_count_after}"
        # 跳转到中心→首页
        center_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-center > div > a > span"))
        )
        center_btn.click()
        index_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#collapse-nav-business > li:nth-child(1) > a"))
        )
        index_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("cart_go_to_index")


# 执行测试
if __name__ == "__main__":
    cart_page = CartPage()
    cart_page.cartTest1()
    # 保留浏览器5秒观察
    time.sleep(5)
    SHPDriver.quit_driver()